import java.awt.*;
import java.awt.event.*;

class FlyweightFactory {
   private static java.util.Hashtable ht = new java.util.Hashtable();
   private static ButtonListener bl = new ButtonListener();
   public static Button makeButton( String num ) {
      if (ht.containsValue( num ))
         return (Button) ht.get( num );        
      Button btn = new Button( num );          
      btn.addActionListener( bl );
      ht.put( num, btn );
      return btn;                              
   } 
}

class ButtonListener implements ActionListener {
	static CompositeHandler handler = new CompositeHandler();
	
   public void actionPerformed( ActionEvent e) {
      Button      btn  = (Button) e.getSource();
      Component[] btns = btn.getParent().getComponents();
      
      int value = Integer.parseInt(btn.getLabel());
      handler.process(value);
   }  
}

class FlyweightDemo {
   public static final int NUM = 15;

   public static void main( String[] args ) {
      Frame frame = new Frame( "Flyweight Demo" );
      frame.setLayout( new GridLayout( NUM, NUM ) );
      for (int i=0; i < NUM; i++)
         for (int j=0; j < NUM; j++)
            frame.add( FlyweightFactory.makeButton( 
               Integer.toString( (int)(i*NUM + j) ) ) );
      frame.pack();
      frame.setVisible( true );      
   }  
}
